import { getPassword,setPassword, getUserName, setUserName,getUserInfo,setUserInfo } from "../../utils/store"

// index.js
const app = getApp()
Page({
  data: {
    sapId:'',
    lastId:'',
    page: null,
  },

  // 事件处理函数
  onLoad(query) {
    this.setData({
        sapId:getUserName(),
        lastId:getPassword(),
        scene: decodeURIComponent(query.scene),
        page: query.page,
        url: query.url,
        urlParams:query.urlParams
    })
  },
  submit(){

  },
  setSapId(e){
    this.setData({
      'sapId' : e.detail.value
    })
  },
  setIdLast(e){
    this.setData({
      'lastId' : e.detail.value
    })
  },

  gotoVisitor(){
    wx.navigateTo({
      url: '/pages/visitorLogin/visitorLogin',
    })
  },

  login(){
    var that =this;
    wx.login({
      success: function (res) {
        console.log('that.data.url',that.data.url)
        if(res.code){
          app.api.getUser(that.data.sapId,that.data.lastId,res.code).then(res => {
            if(res.code == 200){
                if(res.data){
                  console.log('登录完成之后的返回',res.data);
                  setUserInfo(res.data)
                    setUserName(that.data.sapId);
                    setPassword(that.data.lastId);

                    //二维码 或者其他由于需要登录重新定位的页面
                    if(that.data.url){
                      console.log('that.data.url',that.data.url)
                      console.log('that.data.urlParams',that.data.urlParams)
                      wx.reLaunch({
                        url: `/${that.data.url}?urlParams=${that.data.urlParams}`
                      })
                      return
                    }

                    if('riskList' == that.data.page){
                      wx.reLaunch({
                        url: '/pages/risk-list/risk-list'
                      })
                    }else if(that.data.scene){
                      let scenetemp = that.data.scene.toString();
                      if(scenetemp.startsWith('notice')){
                        let qrId = scenetemp.replaceAll('notice?','');
                        wx.reLaunch({
                          url: '/pages/risk-notice/risk-notice?id='+qrId,
                        })
                      }
                      if(scenetemp.startsWith('check')){
                        let itemId = scenetemp.replaceAll('check?','');
                        wx.reLaunch({
                          url: '/pages/check-item/check-item?itemId='+itemId,
                        })
                      }
                      // wx.reLaunch({
                      //   url: '/pages/check-item/check-item?itemId='+that.data.itemId,
                      // })
                    }else{
                      wx.reLaunch({
                        url: '/pages/menu/menu',
                      })
                    }
                }else{
                    wx.showModal({
                        title: '提示',
                        content: '请输入正确的SAP账号和密码',
                        showCancel:false
                      })
                }
            }
          })
        }
      }
    })
    
  }
})